package com.education.service.edu.controller.admin;


import com.education.result.R;
import com.education.service.edu.entity.Chapter;
import com.education.service.edu.entity.vo.ChapterVo;
import com.education.service.edu.service.ChapterService;
import com.education.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Lance
 * @since 2020-06-16
 */
@Slf4j
@Api(tags = "章节管理")
@RestController
@RequestMapping("/admin/edu/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;

    @ApiOperation("添加章节")
    @PostMapping("save")
    public R save(@ApiParam(value = "章节对象", required = true) @RequestBody Chapter chapter){
        boolean result = chapterService.save(chapter);
        if (result){
            return R.ok().message("章节信息已保存成功!");
        }else {
            return R.error().message("抱歉, 章节信息录入失败!");
        }
    }

    @ApiOperation("根据ID查询章节")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value="章节ID", required = true) @PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        if (chapter != null) {
            return R.ok().data("item", chapter);
        } else {
            return R.error().message("抱歉,数据不存在!");
        }
    }

    @ApiOperation("根据ID更新章节信息")
    @PutMapping("update")
    public R update(@ApiParam(value="章节对象", required = true) @RequestBody Chapter chapter){
        boolean result = chapterService.updateById(chapter);
        if (result) {
            return R.ok().message("章节信息更新成功!");
        } else {
            return R.error().message("抱歉,数据不存在!");
        }
    }

    @ApiOperation("根据ID删除章节信息")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "章节ID", required = true) @PathVariable String id){

        //删除视频：VOD
        videoService.removeMediaVideoByChapterId(id);

        boolean result = chapterService.removeChapterById(id);
        if (result) {
            return R.ok().message("章节信息删除成功!");
        } else {
            return R.error().message("数据不存在!");
        }
    }

    @ApiOperation("嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(@ApiParam(value = "课程ID", required = true) @PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }
}

